package ru.magicreader.client.view;

import ru.magicreader.client.I18nConstants;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FeedsPanel extends Composite {
	private final I18nConstants i18nc;
	private FeedsPanelListener listener;
	private ListBox feedsListbox;
	private CheckBox importantCheckbox;
	private VerticalPanel panel;

	public FeedsPanel(I18nConstants i18nc, FeedsPanelListener listener) {
		this.i18nc = i18nc;
		this.listener = listener;
		init();
	}

	private void init() {
		DecoratorPanel decoratorPanel = new DecoratorPanel();
		panel = new VerticalPanel();
		panel.setSpacing(6);
		
		Label userFeedsLabel = new Label(i18nc.yourFeeds() + ":");
		panel.add(userFeedsLabel);
		
		feedsListbox = new ListBox();
		feedsListbox.setVisibleItemCount(5);
		feedsListbox.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				listener.onFeedsSelectionChanged(event);
			}
		});
		panel.add(feedsListbox);
		
		HorizontalPanel userFeedsToolbar = new HorizontalPanel();
		
		Button deleteFeedButton = new Button(i18nc.delete());
		deleteFeedButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				listener.onDeleteButtonClick(event);
			}
		});
		userFeedsToolbar.add(deleteFeedButton);
		
		importantCheckbox = new CheckBox(i18nc.important());
		importantCheckbox.setValue(false);
		importantCheckbox.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				listener.onImportantCheckboxValueChange(event);
			}
		});
		userFeedsToolbar.add(importantCheckbox);
		
		panel.add(userFeedsToolbar);
		
		
		decoratorPanel.add(panel);
		initWidget(decoratorPanel);
	}
	
	public void setListener(FeedsPanelListener listener) {
		this.listener = listener;
	}
	
	public ListBox getFeedsListbox() {
		return feedsListbox;
	}
	
	public CheckBox getImportantCheckbox() {
		return importantCheckbox;
	}
	
	public void setHeight(String height) {
		panel.setHeight(height);
	}

}
